using System;
using System.Data;
using System.Data.SqlClient;

using System.Collections;
using System.Text;

using EntLibEC.Cmn;
using EntLibEC.Info.Sale;



namespace EntLibEC.Dac.Sale
{
	/// <summary>
	/// Summary description for SaleGiftDac.
	/// </summary>
	public class SaleGiftDac
	{
		public SaleGiftDac()
		{
			//
			// TODO: Add constructor logic here
			//
		}

		
		
		public int Insert(SaleGiftInfo oParam)
		{
			string sql = @"INSERT INTO Ent_Sale_Gift
                           (
                           ProductSysNo, GiftSysNo, ListOrder, 
                           CreateUserSysNo)
                           VALUES (
                           @ProductSysNo, @GiftSysNo, @ListOrder, 
                           @CreateUserSysNo)";
			
			SqlCommand cmd = new SqlCommand(sql);

			SqlParameter paramProductSysNo = new SqlParameter("@ProductSysNo", SqlDbType.Int,4);
			SqlParameter paramGiftSysNo = new SqlParameter("@GiftSysNo", SqlDbType.Int,4);
			SqlParameter paramListOrder = new SqlParameter("@ListOrder", SqlDbType.NVarChar,10);
			SqlParameter paramCreateUserSysNo = new SqlParameter("@CreateUserSysNo", SqlDbType.Int,4);

			if ( oParam.ProductSysNo != AppConst.IntNull)
				paramProductSysNo.Value = oParam.ProductSysNo;
			else
				paramProductSysNo.Value = System.DBNull.Value;
			if ( oParam.GiftSysNo != AppConst.IntNull)
				paramGiftSysNo.Value = oParam.GiftSysNo;
			else
				paramGiftSysNo.Value = System.DBNull.Value;
			if ( oParam.ListOrder != AppConst.StringNull)
				paramListOrder.Value = oParam.ListOrder;
			else
				paramListOrder.Value = System.DBNull.Value;
			if ( oParam.CreateUserSysNo != AppConst.IntNull)
				paramCreateUserSysNo.Value = oParam.CreateUserSysNo;
			else
				paramCreateUserSysNo.Value = System.DBNull.Value;

			cmd.Parameters.Add(paramProductSysNo);
			cmd.Parameters.Add(paramGiftSysNo);
			cmd.Parameters.Add(paramListOrder);
			cmd.Parameters.Add(paramCreateUserSysNo);

			return SqlHelper.ExecuteNonQuery(cmd);
		}

		public int Update(Hashtable paramHash)
		{
			StringBuilder sb = new StringBuilder(200);
			sb.Append("UPDATE Ent_Sale_Gift SET ");

			if ( paramHash != null && paramHash.Count != 0 )
			{
				int index = 0;
				foreach(string key in paramHash.Keys)
				{
					object item = paramHash[key];
					if ( key.ToLower()=="sysno" )
						continue;

					if ( index != 0 )
						sb.Append(",");
					index++;

					
					if (item is int || item is decimal)
					{
						sb.Append(key).Append("=").Append(item.ToString());
					}
					else if (item is string)
					{
						sb.Append(key).Append(" = ").Append(Util.ToSqlString(item.ToString()));
					}
					else if (item is DateTime)
					{
						sb.Append(key).Append(" = cast(").Append( Util.ToSqlString(((DateTime)item).ToString(AppConst.DateFormatLong))).Append(" as DateTime)");
					}
				}
			}

			sb.Append(" WHERE SysNo=").Append(paramHash["SysNo"]);

			return SqlHelper.ExecuteNonQuery(sb.ToString());
		}
	}
}
